package com.joy.ogljoy_01.opengl;

public final class Screen {

	/** 屏幕实际宽 */
	public static int SCREEN_W = 0;
	/** 屏幕实际高 */
	public static int SCREEN_H = 0;

	/** 游戏实际宽 1280/800 */
	public final static int GAME_W = 1280;
	/** 游戏实际高 720/480 */
	public final static int GAME_H = 720;

	public static float SCALE = 1F;

	/** 游戏屏幕缩放后的宽 */
	public static int SCALE_W = 0;
	/** 游戏屏幕缩放后的高 */
	public static int SCALE_H = 0;

	public static int OFFSET_X = 0;
	public static int OFFSET_Y = 0;

	public static void adapter(int width, int height) {

		SCREEN_W = width;
		SCREEN_H = height;

		adapter();

	}

	private static void adapter() {
		if (GAME_W * SCREEN_H < SCREEN_W * GAME_H) {
			SCALE = (SCREEN_H * 1.0f) / GAME_H;
		} else if (GAME_W * SCREEN_H == SCREEN_W * GAME_H) {
			SCALE = SCREEN_W * 1.0f / GAME_W;
		} else {
			SCALE = (SCREEN_W * 1.0f) / GAME_W;
		}

		SCALE_W = (int) (GAME_W * SCALE);
		SCALE_H = (int) (GAME_H * SCALE);

		OFFSET_X = ((int) (SCREEN_W - SCALE_W)) >> 1;
		OFFSET_Y = ((int) (SCREEN_H - SCALE_H)) >> 1;
	}

	/**
	 * 把游戏坐标X转换成屏幕实际坐标
	 * 
	 * @param x
	 * @return
	 */
	public static int gameToScreenX(int x) {
		return (int) (OFFSET_X + SCALE * x);
	}

	/**
	 * 把游戏坐标Y转换成屏幕实际坐标
	 * 
	 * @param x
	 * @return
	 */
	public static int gameToScreenY(int y) {
		return (int) (OFFSET_Y + SCALE * y);
	}

	/**
	 * 把游戏内的长度转换成屏幕实际长度
	 * 
	 * @param x
	 * @return
	 */
	public static int gameToScreenWH(int w) {
		return (int) (SCALE * w);
	}

	/**
	 * 把屏幕实际坐标X转换成游戏坐标
	 * 
	 * @param x
	 * @return
	 */
	public static int screenToGameX(float x) {
		return (int) ((x - OFFSET_X) / SCALE);
	}

	/**
	 * 把屏幕实际坐标Y转换成游戏坐标
	 * 
	 * @param x
	 * @return
	 */
	public static int screenToGameY(float y) {
		return (int) ((y - OFFSET_Y) / SCALE);
	}

	/**
	 * 把屏幕实际长度转换成游戏内的长度
	 * 
	 * @param x
	 * @return
	 */
	public static int screenToGameWH(float w) {
		return (int) (w / SCALE);
	}
}
